﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace myMapObj
{
    /// <summary>
    /// Mercato投影
    /// </summary>
    class MercatoProjection
    {

        static double a = 6378245, b = 6356863.0188;  //长半轴短半轴参数
        static double e12 = (a * a - b * b) / (a * a);
        static double e1 = Math.Sqrt(e12);//第一偏心率
        static double e22 = (a * a - b * b) / (b * b);
        static double e2 = Math.Sqrt(e22);//第二偏心率
        static double L0 = 0;//原始经度
        static double B0 = 42 * Math.PI / 180;//标准纬度

        static double K = (a * a / b) / Math.Sqrt(1 + e2 * e2 * Math.Cos(B0) * Math.Cos(B0)) * Math.Cos(B0);

        /// <summary>
        /// 经纬度坐标转XY坐标, 经度对应X，纬度对应Y
        /// </summary>
        /// <param name="coordinate"></param>
        /// <returns></returns>
        public moPoint LngLat2XY(moPoint coordinate)
        {
            double x = Longitude2X(coordinate.X);
            double y = Latitude2Y(coordinate.Y);
            return new moPoint(x, y);
        }       


        static double Latitude2Y(double lat)
        {
            double latRad = Deg2Rad(lat);
            double y = K * Math.Log(Math.Tan(Math.PI / 4 + latRad / 2)) *
                (Math.Pow((1 - e1 * Math.Sin(latRad)) / (1 + e1 * Math.Sin(latRad)), e1 / 2));
            return y;
        }

        static double Longitude2X(double lon)
        {
            double lonRad = Deg2Rad(lon);
            double x = K * (lonRad - L0);
            return x;
        }
       
        /// <summary>
        /// 角度制转弧度制
        /// </summary>
        /// <param name="degree">角度</param>
        /// <returns>弧度</returns>
        static double Deg2Rad(double degree)
        {
            return degree / 180 * Math.PI;
        }

        /// <summary>
        /// 弧度制转角度制
        /// </summary>
        /// <param name="radian">弧度</param>
        /// <returns>角度</returns>
        static double Rad2Deg(double radian)
        {
            return radian * 180 / Math.PI;
        }
        
    }
}
 
